package club.janna.jrpc.remoting.netty;

import club.janna.jrpc.remoting.Channel;
import club.janna.jrpc.remoting.Handler;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Panbo.Guo
 */
@Slf4j
public class NettyServerHandler extends ChannelDuplexHandler {

    private Handler serverHandler;
    private Channel channel;

    public NettyServerHandler(Handler serverHandler) {
        this.serverHandler = serverHandler;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        channel = new NettyChannel(ctx.channel());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        serverHandler.handleReceive(channel, msg);
    }
}
